package homework;


import javax.swing.*;
import java.util.Scanner;

public class StudentUI {
    private Scanner scanner = new Scanner(System.in);
    private SuperA superA = new SuperA();

    //主页面
    public void menu(){
        JOptionPane.showMessageDialog(null,"欢迎使用学生管理系统");

        while (true){
            int index = 0;
            String index01 = JOptionPane.showInputDialog(null,"请选择:1、添加学生信息  2、删除学生信息  3、查询学生信息  4、修改学生信息  5、退出");
            try {
                index = Integer.parseInt(index01);
            }catch (NumberFormatException e){
                JOptionPane.showMessageDialog(null,"请输入正确选项");
                continue;
            }
            switch (index){
                case 1:
                    add();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    lookup();
                    break;
                case 4:
                    revise();
                    break;
                case 5:
                    System.exit(1);
                default:
                    JOptionPane.showMessageDialog(null,"输入有误，请重新输入");
                    System.out.println("输入有误，请重新输入");
                    continue;
            }

        }
    }

    //增加学生
    private void add() {
        String name = JOptionPane.showInputDialog(null, "请输入姓名：");


        String age01 = JOptionPane.showInputDialog(null,"请输入年龄：");
        int age = Integer.parseInt(age01);

        String sex = JOptionPane.showInputDialog(null, "请输入性别：");


        superA.add(new Student(name,age,sex));
        JOptionPane.showMessageDialog(null,"添加成功");
    }

    //删除学生
    private void delete() {
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        for (int i = 0;i < superA.size();i++){
            if (superA.get(i).equals(new Student(name))){
                int delete = JOptionPane.showConfirmDialog(null,"是否确认删除？");
                if (delete == 0){
                    superA.remove(i);
                    JOptionPane.showMessageDialog(null,"删除成功");
                    return;
                }
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }

    //查询学生
    private void lookup() {
        int input = 0;
        String input01 = JOptionPane.showInputDialog(null,"请输入:(输入1、查询所有学生 2、查询学生姓名)");
        input = Integer.parseInt(input01);
        try {
            if (input == 1){
                for (int i = 0;i < superA.size();i++){
                    JOptionPane.showMessageDialog(null,superA.get(i));
                }
            }else if (input == 2){
                String name = JOptionPane.showInputDialog(null,"请输入学生名字：");
                for (int i = 0; i < superA.size(); i++){
                    if (superA.get(i).equals(new Student(name))){
                        JOptionPane.showMessageDialog(null,superA.get(i));
                        System.out.println(superA.get(i));
                        return;
                    }
                }
            }else {
                JOptionPane.showMessageDialog(null,"查无此人");
            }

        }catch (NumberFormatException e){
            System.out.println("请输入‘1’或者‘2’");
        }
    }

        //修改学生
    private void revise() {
        System.out.println("请输入需要修改的学生姓名：");
        String name = JOptionPane.showInputDialog(null,"请输入需要修改的学生姓名：");
        for (int i = 0; i < superA.size(); i++){
            Student student =  (Student) superA.get(i);
            if (student.getName().equals(name)){
                String newName = JOptionPane.showInputDialog(null,"请输入新学生的姓名: ");

                String newAge01 = JOptionPane.showInputDialog(null,"请输入新学生的年龄: ");
                 int newAge = Integer.parseInt(newAge01);

                String newSex = JOptionPane.showInputDialog(null,"请输入新学生的性别: ");

                student.setName(newName);
                student.setAge(newAge);
                student.setSex(newSex);

                JOptionPane.showMessageDialog(null,"修改成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }

    public static void main(String[] args) {
        StudentUI studentUI = new StudentUI();
        studentUI.menu();
    }
}
